#include <graphics.h>
#include <conio.h>
#include <math.h>

#define PI 3.14159
#define High 480
#define Width 640

int main()
{
	initgraph(Width, High);

	//中心坐标
	int center_x, center_y;
	center_x = Width / 2;
	center_y = High / 2;

	//针长度
	int secondLength,minuteLength,hourLength;
	secondLength = Width / 5;
	minuteLength = Width / 6;
	hourLength = Width / 7;

	int secondEnd_x, secondEnd_y;
	int minuteEnd_x, minuteEnd_y;
	int hourEnd_x, hourEnd_y;

	secondEnd_x = center_x + secondLength;
	secondEnd_y = center_y;

	minuteEnd_x = center_x + minuteLength;
	minuteEnd_y = center_y;

	hourEnd_x = center_x + hourLength;
	hourEnd_y = center_y;

	float secondAngle = 0;			//秒针转动角度
	float minuteAngle = 0;			//分针转动角度
	float hourAngle = 0;			//时针转动角度

	SYSTEMTIME ti;					//得到系统时间

	//绘制
	BeginBatchDraw();
	while (1)
	{
		GetLocalTime(&ti);
		secondAngle = ti.wSecond * 2 * PI / 60;
		minuteAngle = ti.wMinute * 2 * PI / 60;
		hourAngle = ti.wHour * 2 * PI / 12;
		
		//表盘及刻度
		setcolor(WHITE);
		circle(center_x, center_y, Width/4);
		int i,x,y;
		for (i = 0; i < 60; i++)
		{
			x = center_x + (Width / 4.5)*sin(2 * PI*i / 60);
			y = center_y - (Width / 4.5)*cos(2 * PI*i / 60);
			if (i % 15 == 0)
				fillrectangle(x, y, x + 4, y + 4);
			else if (i % 5 == 0)
				circle(x, y, 3);
			else
				putpixel(x, y, WHITE);
		}
		//秒
		secondEnd_x = center_x + secondLength * sin(secondAngle);
		secondEnd_y = center_y - secondLength * cos(secondAngle);

		//分
		minuteEnd_x = center_x + minuteLength * sin(minuteAngle);
		minuteEnd_y = center_y - minuteLength * cos(minuteAngle);

		//时
		hourEnd_x = center_x + hourLength * sin(hourAngle);
		hourEnd_y = center_y - hourLength * cos(hourAngle);

		//秒
		setlinestyle(PS_SOLID, 1);
		setcolor(WHITE);
		line(center_x, center_y, secondEnd_x, secondEnd_y);

		//分
		setlinestyle(PS_SOLID, 2);
		setcolor(BLUE);
		line(center_x, center_y, minuteEnd_x, minuteEnd_y);

		//时
		setlinestyle(PS_SOLID, 3);
		setcolor(RED);
		line(center_x, center_y, hourEnd_x, hourEnd_y);

		outtextxy(center_x-30, center_y + Width / 6, _T("我的时钟"));

		FlushBatchDraw();
		Sleep(100);

		setcolor(BLACK);
		line(center_x, center_y, secondEnd_x, secondEnd_y);
		line(center_x, center_y, minuteEnd_x, minuteEnd_y);
		line(center_x, center_y, hourEnd_x, hourEnd_y);

	}
	EndBatchDraw();


	getch();
	closegraph();
	return 0;
}